package com.itfreer.http;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.itfreer.http.request.JsonArrayPostRequest;
import com.itfreer.http.request.JsonObjectPostRequest;
import com.itfreer.utils.JSONUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义通用数据访问服务
 */
public class DataService {

    protected String baseUrl = "http://192.168.21.150:8081/WaterMonitor/android/";

    protected Context context;

    public static final DataService dataServer = new DataService();

    private DataService() {
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void login(String userName, String password, Response.Listener<Map> listener,
                      Response.ErrorListener errorListener) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("userName", userName);
            params.put("password", password);

            RequestQueue requestQueue = Volley.newRequestQueue(context);
            JsonObjectPostRequest jsonObjectPostRequest =
                    new JsonObjectPostRequest(baseUrl + "water/login",
                            listener, errorListener, params);
            requestQueue.add(jsonObjectPostRequest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 保存数据
     *
     * @param tableName
     * @param row
     * @param listener
     * @param errorListener
     */
    public void save(String tableName, Map row,
                     Response.Listener<Map> listener,
                     Response.ErrorListener errorListener) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("tableName", tableName);
            params.put("row", JSONUtils.toJSONString(row));

            RequestQueue requestQueue = Volley.newRequestQueue(context);
            JsonObjectPostRequest jsonObjectPostRequest =
                    new JsonObjectPostRequest(baseUrl + "save",
                            listener, errorListener, params);
            requestQueue.add(jsonObjectPostRequest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 删除数据
     *
     * @param tableName
     * @param whereMap
     * @param listener
     * @param errorListener
     */
    public void delete(String tableName, Map whereMap,
                       Response.Listener<Map> listener,
                       Response.ErrorListener errorListener) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("tableName", tableName);
            params.put("where", JSONUtils.toJSONString(whereMap));

            RequestQueue requestQueue = Volley.newRequestQueue(context);
            JsonObjectPostRequest jsonObjectPostRequest =
                    new JsonObjectPostRequest(baseUrl + "delete",
                            listener, errorListener, params);
            requestQueue.add(jsonObjectPostRequest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 分页查询
     *
     * @param tableName
     * @param query
     * @param order
     * @param pageSize
     * @param pageIndex
     * @param listener
     * @param errorListener
     */
    public void pageSelect(String tableName, Map query, Map order,
                           int pageSize, int pageIndex,
                           Response.Listener<List<Map>> listener,
                           Response.ErrorListener errorListener) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("tableName", tableName);
            params.put("query", JSONUtils.toJSONString(query));
            params.put("order", JSONUtils.toJSONString(order));
            params.put("pageSize", pageSize);
            params.put("pageIndex", pageIndex);

            RequestQueue requestQueue = Volley.newRequestQueue(context);
            JsonArrayPostRequest jsonObjectPostRequest =
                    new JsonArrayPostRequest(baseUrl + "pageSelect",
                            listener, errorListener, params);
            requestQueue.add(jsonObjectPostRequest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}